// pages/leftoverStock/leftoverStock.js 
const app = getApp();
import httpsUtils from '../../utils/https.js';
import commonUtils from '../../utils/common.js';
import myUtils from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftoverStockList: [],
    leftoverLength: 1,
    areaList: [],
    showOrderingInArea: true,
    orderMethod: [
      { index: 0, name: '综合排序' },
      { index: 1, name: '发布时间' },
      { index: 2, name: '价格最高' },
      { index: 3, name: '价格最低' },
      { index: 4, name: '收藏最多' },
      { index: 5, name: '销量' },
    ],
    showOrderingInMethod: true,//显示尾货选择方式
    classList: [],
    secondClass: [],
    showClass: true,

    sqList: [],
    flList: [],
    flTwoList: [],
    sqShowStatus: false,
    flShowStatus: false,
    pxShowStatus: false,
    areaId: "",
    goodType: "",
    goodParentType: "",
    searchType: "2",
    sqName: "全部商圈",
    flName: "全部分类",
    pxName: "发布时间",
    sqUrl: "../../static/image/x_stoc/sanjiao.png",
    flUrl: "../../static/image/x_stoc/sanjiao.png",
    pxUrl: "../../static/image/x_stoc/sanjiao.png",
    current: 1,
    hasNextPage: true,

    column: 2, //显示列数
    isClass: false, //是否显示分类选择
    leftType: 1, //尾货类型，1表示尾货信息，2表示尾货市场

    tailGoodsList: [],
    tailGoodsLenght: 1,
    tailGoodsCurrent: 1,
    tailGoodsHasNextPage: true,
  },
  /*发布*/
  askToBuyIssue: function (e) {
    //获取用户信息
    commonUtils.getUserInfoByToken(wx.getStorageSync('utoken'), 10, function () {
      wx.navigateTo({
        url: '../askToBuyTail/askToBuyTail',
      })
    });
  },
  /**
   * 切换类型
   */
  switchType: function (e) {
    this.setData({
      leftType: e.currentTarget.dataset.type
    })
  },
  /**
   * 取消选择
   */
  cancelSelect: function () {
    this.setData({
      isClass: false,
    })
  },
  /**
   * 切换显示行数
   */
  checkCol: function (e) {
    var column = this.data.column + 1;
    column = column > 4 ? 2 : column;
    var totast = "";
    if (column == 2){
      totast = "一行两列"
    }
    if (column == 3) {
      totast = "一行三列"
    }
    if (column == 4) {
      totast = "一行四列"
    }
    wx.showToast({
      title: totast,
      icon:"none"
    })
    this.setData({
      column: column
    })
  },
  /**
   * 跳转首页
   */
  toindex: function () {
    wx.switchTab({
      url: '../index/index',
    })
  },
  /*选择商圈下拉列表*/
  sqSelect: function (e) {
    var that = this;
    that.setData({
      areaId: e.currentTarget.dataset.areaid,
      current: 1,
      hasNextPage: true,
      leftoverStockList: [],
      tailGoodsList: [],
      tailGoodsCurrent: 1,
      tailGoodsHasNextPage: true,
    })
    /**刷新当前页面 */
    that.leftGoods();

    /* 尾货信息列表 */
    this.tailGoods();
  },

  /*选择分类*/
  flSelect: function (e) {
    var that = this;
    var flId = e.currentTarget.dataset.typeOneId;
    var flList = that.data.flList;
    for (let obj of flList) {
      if (flId == obj.id) {
        obj.select = !obj.select;
      }
    }
    that.setData({
      flList: flList
    })
  },

  /*选择二级分类*/
  flTwoTypeSelect: function (e) {
    var that = this;
    var typeId = e.currentTarget.dataset.typeTwoId;
    var flList = that.data.flList;
    for (let obj of flList) {
      for (let fl of obj.sortTypeList) {
        if (typeId == fl.id) {
          fl.select = !fl.select;
        }
      }
    }
    that.setData({
      flList: flList
    })
  },
  /**
   * 打开选择分类
   */
  openSelect: function (e) {
    this.setData({
      isClass: true
    })
  },
  /**
   * 确认选择分类
   */
  sureSelect: function (e) {
    this.setData({
      isClass: false,
      current: 1,
      hasNextPage: true,
      leftoverStockList: [],
      tailGoodsList: [],
      tailGoodsCurrent: 1,
      tailGoodsHasNextPage: true,
    })
    /* 查询尾货*/
    this.leftGoods();

    /* 尾货信息列表 */
    this.tailGoods();
  },
  /**
   * 全部分类
   */
  allSelect: function (e) {
    var that = this;
    var flList = that.data.flList;
    for (let obj of flList) {
      obj.select = false;
      for (let fl of obj.sortTypeList) {
        fl.select = false;
      }
    }
    this.setData({
      isClass: false,
      current: 1,
      hasNextPage: true,
      leftoverStockList: [],
      flList: flList,
      tailGoodsList: [],
      tailGoodsCurrent: 1,
      tailGoodsHasNextPage: true,     
    })
    /* 查询尾货 */
    this.leftGoods();

    /* 尾货信息列表 */
    this.tailGoods();
  },

  /**
   * 获取商圈列表
   */
  selectAreaList: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        for (let obj of json.data) {
          obj.areaName = obj.areaName.replace("商圈", "");
        }
        that.setData({
          sqList: json.data
        })
      }
    })
  },

  /**
   * 获取分类列表
   */
  selectTypeList: function (e) { 
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/sortType/selectTwoList',
      data: {},
    }, function (json) {
      for (let obj of json.data.list) {
        obj.select = false;
        if (obj.sortTypeList.length >0){
          for (let fl of obj.sortTypeList) {
            fl.select = false;
          }
        }
      }
      if (json.data && json.success) {
        that.setData({
          flList: json.data.list
        })
      }
    })
  },

  /**
  * 尾货列表
  */
  leftGoods: function (e) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })
    setTimeout(function () {
      wx.hideToast()
    }, 5000)
    var that = this;

    //获取分类id
    var sortTypeIds = [];
    for (let obj of that.data.flList) {
      for (let fl of obj.sortTypeList) {
        if (fl.select) {
          sortTypeIds.push(fl.id)
        }
      }
    }
    /*  尾货列表 */
    httpsUtils.getData({
      _url: app.globalData._url + '/poop/selectPoopList?current=' + that.data.current + '&tradeAreaId=' + that.data.areaId + "&sortTypeId=" + that.data.goodType + "&sort=" + that.data.searchType + "&sortTypeIds=" + myUtils.concatStr(sortTypeIds, ","),
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        //console.log(json.data)
        that.setData({
          leftoverStockList: that.data.leftoverStockList.concat(json.data.list),
          leftoverLength: json.data.total,
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
        })
      }
      wx.hideToast();
    })
  },

  /**
   * 尾货信息列表
   */
  tailGoods: function (e) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })
    setTimeout(function () {
      wx.hideToast()
    }, 5000)
    var that = this;

    //获取分类id
    var sortTypeIds = [];
    for (let obj of that.data.flList) {
      for (let fl of obj.sortTypeList) {
        if (fl.select) {
          sortTypeIds.push(fl.id)
        }
      }
    }

    httpsUtils.getData({
      _url: app.globalData._url + '/tailGoods/selectList?current=' + that.data.tailGoodsCurrent + '&tradeAreaId=' + that.data.areaId + "&sortTypeId=" + that.data.goodType + "&sort=" + that.data.searchType + "&sortTypeIds=" + myUtils.concatStr(sortTypeIds, ","),
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        var tailGoodsList = that.data.tailGoodsList.concat(json.data.list);
        for (let obj of tailGoodsList) {
          var timeStr = that.timeFormat(obj.gmtDatetime)

          if (timeStr.split(" ").length > 1) {
            obj.time2 = timeStr + "发布";
            obj.time3 = timeStr.split(" ")[0] + "发布";
            obj.time4 = timeStr.split(" ")[0] + "发布";

          } else {
            obj.time2 = timeStr;
            obj.time3 = timeStr;
            obj.time4 = timeStr;
          }
        }
        that.setData({
          tailGoodsList: tailGoodsList,
          tailGoodsLenght: json.data.total,
          tailGoodsCurrent: json.data.pageNum,
          tailGoodsHasNextPage: json.data.hasNextPage,
        })
      }
      wx.hideToast();
    })
  },
  /**
   * 时间转换
   */
  timeFormat: function (time) {
    var that = this;
    var nowTime = Date.parse(new Date());
    var subTime = (nowTime - Date.parse(new Date(time))) / 1000;
    var timeStr = '';
    if (subTime < 60) {
      timeStr = '刚刚发布';
    } else if (parseInt(subTime / 60) < 60) {
      timeStr = parseInt((subTime / 60)) + '分钟前发布';
    } else if (parseInt(subTime / (60 * 60)) < 24) {
      timeStr = parseInt(subTime / (60 * 60)) + '小时前发布';
    } else if (parseInt(subTime / (60 * 60 * 24)) < 2) {
      timeStr = '昨天' + time.split(" ")[1] + "发布";
    } else if (parseInt(subTime / (60 * 60 * 24)) < 3) {
      timeStr = '前天' + time.split(" ")[1] + "发布";
    } else {
      timeStr = time;
    }
    return timeStr;
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    /* 初始化尾货列表 */
    that.leftGoods();

    /* 初始化商圈列表 */
    that.selectAreaList();

    /* 获取分类列表 */
    that.selectTypeList();

    /* 初始化尾货列表 */
    that.leftGoods();

    /* 尾货信息列表 */
    that.tailGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isFlush == true) {
      app.globalData.isFlush = false;
      var that = this;
      that.setData({
        current: 1,
        hasNextPage: true,
        leftoverStockList: [],

        tailGoodsList: [],
        tailGoodsCurrent: 1,
        tailGoodsHasNextPage: true,
      })

      /* 初始化尾货列表 */
      that.leftGoods();

      /* 尾货信息列表 */
      that.tailGoods();
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      leftoverStockList: [],
      leftoverLength: 1,
      areaList: [],
      showOrderingInArea: true,
      orderMethod: [
        { index: 0, name: '综合排序' },
        { index: 1, name: '发布时间' },
        { index: 2, name: '价格最高' },
        { index: 3, name: '价格最低' },
        { index: 4, name: '收藏最多' },
        { index: 5, name: '销量' },
      ],
      showOrderingInMethod: true,//显示尾货选择方式
      classList: [],
      secondClass: [],
      showClass: true,

      sqList: [],
      flList: [],
      flTwoList: [],
      sqShowStatus: false,
      flShowStatus: false,
      pxShowStatus: false,
      areaId: "",
      goodType: "",
      goodParentType: "",
      searchType: "2",
      sqName: "全部商圈",
      flName: "全部分类",
      pxName: "发布时间",
      sqUrl: "../../static/image/x_stoc/sanjiao.png",
      flUrl: "../../static/image/x_stoc/sanjiao.png",
      pxUrl: "../../static/image/x_stoc/sanjiao.png",
      current: 1,
      hasNextPage: true,
      isClass: false, //是否显示分类选择

      tailGoodsList: [],
      tailGoodsLenght: 1,
      tailGoodsCurrent: 1,
      tailGoodsHasNextPage: true,
    })

    /* 初始化尾货列表 */
    that.leftGoods();

    /* 初始化商圈列表 */
    that.selectAreaList();

    /* 获取分类列表 */
    that.selectTypeList(); 

    /* 初始化尾货列表 */
    that.leftGoods();

    /* 尾货信息列表 */
    that.tailGoods();

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.leftType == 2){
      if (that.data.hasNextPage) {
        that.setData({
          current: that.data.current + 1,
        })
        /*尾货列表*/
        that.leftGoods();
      }
    }else{
      if (that.data.tailGoodsHasNextPage) {
        that.setData({
          tailGoodsCurrent: that.data.tailGoodsCurrent + 1,
        })
        /*尾货信息*/
        that.tailGoods();
      }
    }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '服装尾货 跳楼价格',
      path: 'pages/leftoverStock/leftoverStock',
    }
  },
  allArea: function (options) {
    var that = this;
    that.getData({
      _url: app.globalData._url + '/poop/selectPoopList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          leftoverStockList: json.data.list,
          showOrderingInArea: true,
        })
      } else {
        wx.showModal({
          title: '数据飞到外太空去了',
        })
      }
    })
  },
  // 显示商圈下选中
  showAreaOrder: function (e) {
    var that = this;
    that.setData({
      showOrderingInArea: false,
    })
  },
  /*点击商圈选中*/
  pitchOnBussCir: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    //设置当前样式
    that.setData({
      'currentItem': id,
      showBussCircle: false,
      showOrderingInArea: true,
    })
    that.getData({
      _url: app.globalData._url + '/poop/selectPoopList?propertyTypeId=' + id,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        //console.log(json.data)
        that.setData({
          leftoverStockList: json.data.list,
        })
      } else {
        wx.showModal({
          title: '失败',
          content: '',
        })
      }
    })
  },
  // 显示分类部分
  showClassSelect: function (e) {
    var that = this;
    that.setData({
      showClass: false,
    })
  },
  /*点击一级分类选中*/
  pitchOnFirstClass: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    //设置当前样式
    that.setData({
      'currentFirstClassItem': id,
    })
    that.getData({
      _url: app.globalData._url + '/sortType/selectTwoList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        for (var s = 0; s < json.data.list.length; s++) {
          if (id == json.data.list[s].id) {
            var second_class = json.data.list[s].sortTypeList;
            that.setData({
              secondClass: second_class,
            })
          }
        }
        that.setData({
          secondClassList: json.data.list,
        })
      } else {
        wx.showModal({
          title: '失败',
          content: '',
        })
      }
    })
  },
  /*点击二级分类选中*/
  pitchOnSecondClass: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    //设置当前样式
    that.setData({
      'currentSecondClassItem': id,
      showClass: true,
    })
    that.getData({
      _url: app.globalData._url + '/poop/selectPoopList?sortTypeId=' + id,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          leftoverStockList: json.data.list,
        })
      } else {
        wx.showModal({
          title: '失败',
          content: '',
        })
      }
    })
  },
  //显示方式排序
  showMethodOrder: function (options) {
    var that = this;
    that.setData({
      showOrderingInMethod: false,
    })
  },
  pitchOnMethod: function (options) {
    var that = this;
    var indexs = options.currentTarget.dataset.index;
    var inx = indexs + 1;
    that.setData({
      'currentMethodItem': indexs,
      showOrderingInMethod: true,
    })
    that.getData({
      _url: app.globalData._url + '/poop/selectPoopList?sort=' + inx,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        //console.log(json.data)
        that.setData({
          leftoverStockList: json.data.list,
        })
      } else {
        wx.showModal({
          title: '失败',
          content: '',
        })
      }
    })
  },
  getData: function (params, cb) {
    // console.log(params)
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})